package Chapter3;

import Chapter2.Node;

public class SetOfStacks {
	int StackMaxSize = 8; //const
	int stackSize = 256; //const
	MyStack stackBase[];
	
	int stackNodeNumber = 0;
	int stackNumber = 0;
	
	public SetOfStacks()
	{
		stackBase = new MyStack[stackSize];
	}
	public void push(int value)
	{
		int stackIndex = getStackIndex();
		if(stackBase[stackIndex] == null)
		{
			stackBase[stackIndex] = new MyStack();
			stackNumber++;
		}
		stackBase[getStackIndex()].push(value);
		stackNodeNumber ++;
	}
	
	public Node pop()
	{
		stackBase[getStackIndex()].pop();
		stackNodeNumber --;
		return null;
	}
	
	public Node popAt(int index)
	{
		if(index < stackNumber){
			Node result = stackBase[index].pop();
			Node bottom;
			for(int i = index + 1; i < stackNumber; i++){
				bottom = stackBase[i].removeBottom();
				stackBase[i-1].push(bottom.value);
			}
			return result;
		}
		return null;
	}
	
	public void checkResult()
	{
		for(int i = 0; i < stackNumber; i++)
		{
			stackBase[i].checkStackResult();
		}
	}
	private int getStackIndex()
	{
		return (int)(stackNodeNumber/StackMaxSize) ;
	}
}
